package com.study.d012Components;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * MenuBar--> Menu --> MenuItem
 * SeparatorMenuItem  菜单分隔符
 * ToggleGroup， RadioMenuItem 单选菜单
 * CheckMenuItem  复选菜单
 * CustomMenuItem 自定义菜单 和MenuItem的用法类似
 * MenuButton   带下拉框的菜单栏（不需要点击下拉按钮就能下拉）
 * SplitMenuButton  带下拉框的菜单栏（需要点击下拉按钮才能下拉）
 * ContextMenu 右键菜单  （需要绑定到Node上面）
 * @author Administrator
 *
 */
public class MenuBarMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		BorderPane borderPane = new BorderPane();

		MenuBar menuBar = new MenuBar();

		Menu fileMenu = new Menu("File");
		Menu editMenu = new Menu("Edit");
		Menu sourceMenu = new Menu("Source");
		Menu smbMenu = new Menu("SplitMenuButton");

		MenuItem newItem = new MenuItem("New");
		MenuItem openItem = new MenuItem("Open File...");
		MenuItem closeItem = new MenuItem("Close");
		MenuItem closeAllItem = new MenuItem("Close All");
		MenuItem saveItem = new MenuItem("Save");

		SeparatorMenuItem spItem1 = new SeparatorMenuItem();
		SeparatorMenuItem spItem2 = new SeparatorMenuItem();

		//CustomMenuItem自定义菜单
		CustomMenuItem cusm=new CustomMenuItem(new Button("自定义菜单"));



		fileMenu.getItems().addAll(newItem,spItem1,openItem,closeItem,spItem2,closeAllItem,saveItem,cusm);

		//添加三级菜单
		Menu menu2= new Menu("用户管理");
		MenuItem addUserItem = new MenuItem("新增用户管理");
		MenuItem updateUserItem = new MenuItem("修改用户管理");
		MenuItem delUserItem = new MenuItem("删除用户管理");
		menu2.getItems().addAll(addUserItem,updateUserItem,delUserItem);
		fileMenu.getItems().add(menu2);

		menuBar.getMenus().addAll(fileMenu,editMenu,sourceMenu);


		borderPane.setTop(menuBar);

		stage.setScene(new Scene(borderPane,500,400));
		stage.show();

		//添加菜单事件
		saveItem.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				System.out.println("saveItem....");
			}
		});

		//添加快捷键
		KeyCombination key = new KeyCodeCombination(KeyCode.K,KeyCombination.CONTROL_DOWN);
		saveItem.setAccelerator(key);

		stage.getScene().getAccelerators().put(key, new Runnable() {

			@Override
			public void run() {
				saveItem.fire();
			}
		});

		//菜单上添加图标
		ImageView imageView = new ImageView(new Image("/icon/01.jpg", 15, 15, true, true));
		saveItem.setGraphic(imageView);


		//单选菜单
		ToggleGroup group = new ToggleGroup();
		RadioMenuItem rmi1 = new RadioMenuItem("radioItem1");
		RadioMenuItem rmi2 = new RadioMenuItem("radioItem2");
		RadioMenuItem rmi3 = new RadioMenuItem("radioItem3");

		rmi1.setToggleGroup(group);
		rmi2.setToggleGroup(group);
		rmi3.setToggleGroup(group);
		editMenu.getItems().addAll(rmi1,rmi2,rmi3);

		//单选菜单点击事件
		rmi1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				System.out.println("rmi1是否被选中:" + rmi1.isSelected());
			}
		});

		CheckMenuItem cmi1 = new CheckMenuItem("cmiItem1");
		CheckMenuItem cmi2 = new CheckMenuItem("cmiItem2");
		CheckMenuItem cmi3 = new CheckMenuItem("cmiItem3");
		sourceMenu.getItems().addAll(cmi1,cmi2,cmi3);

		//复选菜单点击事件
		cmi1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				System.out.println("cmi1是否被选中：" + cmi1.isSelected());
			}
		});


		//MenuButton的使用
		MenuButton mb = new MenuButton("MenuButton");
		mb.getItems().addAll(new MenuItem("MenuButton1"),new MenuItem("MenuButton2"),new MenuItem("MenuButton3"));
		borderPane.setLeft(mb);


		//SplitMenuButton的使用
		SplitMenuButton smb=new SplitMenuButton();
		smb.setText("关机");
		smb.getItems().addAll(new MenuItem("休眠"),new MenuItem("重启"));
		borderPane.setRight(smb);

		//右键菜单
		Button rightButton = new Button("右键菜单");
		borderPane.setCenter(rightButton);

		ContextMenu cm = new ContextMenu();
		cm.getItems().addAll(new MenuItem("右键菜单1"),new MenuItem("右键菜单2"),new MenuItem("右键菜单3"),new MenuItem("右键菜单4"));
		rightButton.setContextMenu(cm);

		//监听rightButton按钮的右键点击事件
		rightButton.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
			@Override
			public void handle(ContextMenuEvent event) {
				System.out.println("显示右键菜单");
			}
		});
	}
}
